package config;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.PrintWriter;
import java.util.UUID;

import room.Room;
import threads.RunGameThread;

import app.AppGlobals;

public class LauncherHelper {
	

    public static void generateConfig(Room room)
    {
    	
    	room.roomName = (room.roomName == "") ? generateRandomRoomName() : room.roomName;
        LauncherConfig config = AppGlobals.launcherConfig;
        File file = new File(AppGlobals.launcherConfig.launcherDir + "//system.CONF");
        if(!file.exists()) return;
        
		try {
			PrintWriter writer = new PrintWriter(file);
			writer.println("#config file");
	        writer.println("#nickname & gamename should be less than 20 characters");
	        writer.println("#Generated using " + room.roomName);
	        writer.println("use_d3d = " + ((config.enabled3D == true) ? 1 : 0));
	        writer.println("antialias = " + config.antialias);
	        writer.println("errorlog = 1");
	        writer.println("nickname = " + AppGlobals.localUserName + "$" + AppGlobals.localUser.loginKey);
	        writer.println("gamename = " + room.generateGameName() + room.roomName);
	        writer.println("roompass =");
	        writer.println("lastdeck = " + config.defaultDeck);
	        writer.println("textfont = fonts/" + config.gameFont + " " + config.fontSize);
	        writer.println("numfont = fonts/arialbd.ttf");
	        writer.println(("serverport = " +  config.gamePort));
	        writer.println("lastip = " +  config.serverAddress);
	        writer.println("lastport = " +  config.gamePort);
	        writer.println("fullscreen = " +  ((config.fullscreen == true) ? 1 : 0));
	        writer.println("enable_sound = " + ((config.enableSound == true) ? 1 : 0));
	        writer.println(("enable_music = " + ((config.enableMusic == true) ? 1 : 0)));
	        writer.println("use_skin = " +  ((config.useSkin == true) ? 1 : 0));
	        writer.println("auto_card_placing = " +  ((config.autoPlacing == true) ? 1 : 0));
	        writer.println("random_card_placing = " +  ((config.randomPlacing == true) ? 1 : 0));
	        writer.println("auto_chain_order = " + ((config.autoChain == true) ? 1 : 0));
	        writer.println("no_delay_for_chain = " + ((config.noChainDelay == true) ? 1 : 0));
	        writer.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }
    
    public static String generateRandomRoomName(){
		
    	String uuid = UUID.randomUUID().toString();
    	
    	System.out.println("uuid = " + uuid.substring(0, 6));
    	return uuid.substring(0, 4);
    }
    
    public static void runGame(String arg){
    	RunGameThread t1 =   new RunGameThread(arg);
    	t1.start();

    }
    public static void runGame(){
    	runGame("");
    }
    public static void openPageInDefaultBrowser(String url) {
    	         try {
					java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    	}


}
